﻿using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.EntityFrameworkCore;
using Ornament.ImportHelper;

namespace Fee.IO.Importers
{
    public class FrameworkImporter<T> : BaseImporter<T> where T : FeeDbContext
    {
        private const int NoPost = 0;
        private const int NamePos = 1;
        //private const int ProjectPos = 2;
        private const int 年份 = 2;
        private const int VendorPost = 3;
        private const int ContractDescPos = 4;
        private const int 专业 = 5;
        private const int ContractTypePos = 6;
        private const int AmoutnPos = 7;
        private const int AmountWithoutTax = 8;
        private const int ReducePos = 9;
        private const int SignTimePos = 10;
        private const int ExpreTimePos = 11;
        private const int StatusPos = 12;

        public FrameworkImporter(T context) : base(context, true)
        {
        }

        protected override bool ConverTo(IList<string> rowStringInfo, out ImportResultItem item)
        {
            //合同号	合同名称	供应商	合同描述	合同类型	投资金额（含税，单位万元）	投资金额（不含税，单位万元）	降点	签订时间	结束时间	状态

            item = new ImportResultItem();

            var no = rowStringInfo.ImportString(NoPost, "合同号", item);


            var result = Context.FrameworkContracts

                             .FirstOrDefault(_ => _.No == no) ?? new FrameworkContract()
                             {
                                 No = no,
                             };


            result.Name = rowStringInfo.ImportString(NamePos, "合同名称", item);
            result.EffectYear = rowStringInfo.ImportString(年份, nameof(年份), item);
            result.Vendor = rowStringInfo.ImportVendor(VendorPost, "供应商", item, Context);
            result.ContractDescription = rowStringInfo.ImportString(ContractDescPos, "合同描述", item, false);
            result.NetType = rowStringInfo.ImportString(专业, nameof(专业), item);
            result.ContractType = rowStringInfo.ImportEnum<ContractType>(ContractTypePos, "合同类型", item, true);
            result.Amount = rowStringInfo.ImportDecimal(AmoutnPos, "投资金额", item, false);
            result.AmountWithoutTax = rowStringInfo.ImportDecimal(AmountWithoutTax, "投资金额(不含税)", item, false);
            result.ReductionAmount = rowStringInfo.ImportDecimal(ReducePos, "降点(不含税)", item, false);
            result.SignTime = rowStringInfo.ImportDatetimeNullable(SignTimePos, "签订时间", item, false);
            result.ExpireTime = rowStringInfo.ImportDatetimeNullable(ExpreTimePos, "结束时间", item, false);
            result.Status = rowStringInfo.ImportEnum<ContractStatus>(StatusPos, "状态", item, false);

            if (!item.HasError)
            {
                item.Data.Add(result);
                return true;
            }

            return false;
        }


    }
}